package com.apex.crm.controller;

import com.apex.crm.base.BaseController;
import com.apex.crm.base.ResultInfo;
import com.apex.crm.query.CustomerQuery;
import com.apex.crm.service.CustomerLinkmanService;
import com.apex.crm.service.CustomerOrderService;
import com.apex.crm.service.CustomerService;
import com.apex.crm.vo.Customer;
import com.apex.crm.vo.CustomerLinkman;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname CustomerController
 * @description TODO
 * @date 2021/6/9 14:26
 */


@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Resource
    private CustomerService customerService;

    //客户联系人
    @Resource
    private CustomerLinkmanService customerLinkmanService;

    @Resource
    private CustomerOrderService customerOrderService;

    @GetMapping("index")
    public String index(){
        return "customer/customer";
    }

    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomersByParams(CustomerQuery customerQuery){
        return customerService.queryCustomersByParams(customerQuery);
    }

    @PostMapping("save")
    @ResponseBody
    public ResultInfo saveCustomer(Customer customer){
        customerService.saveCustomer(customer);
        return  success("客户记录添加成功!");
    }

    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
        return  success("客户记录更新成功!");
    }

    @GetMapping("addOrUpdateCustomerPage")
    public String addOrUpdateCustomerPage(Integer id, Model model){
        model.addAttribute("customer",customerService.selectByPrimaryKey(id));
        //System.out.println(customerService.getById(id));
        return "customer/add_update";
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomer(Integer id){
        customerService.deleteCustomer(id);
        return success("客户记录删除成功!");
    }


    @GetMapping("linkCustomerPage")
    public String linkCustomerPage(Integer cid, Model model){
        //model.addAttribute("customer",customerLinkmanService.getOne(new QueryWrapper<CustomerLinkman>().eq("cus_id",cid)));
        model.addAttribute("customer",customerLinkmanService.selectByCusId(cid));
        //System.out.println(customerLinkmanService.getOne(new QueryWrapper<CustomerLinkman>().eq("cus_id",cid)));
        return "customer/customer_link";
    }

    @PostMapping("updateCustomerLinkman")
    @ResponseBody
    public ResultInfo updateCustomerLinkman(CustomerLinkman customerLinkman){
        //System.out.println(customerLinkman);
        customerLinkmanService.updateCustomerLinkman(customerLinkman);
        return success("联系人信息修改成功");
    }

    @GetMapping("orderInfoPage")
    public String orderInfoPage(Integer cid, Model model){
        model.addAttribute("customer",customerService.selectByPrimaryKey(cid));
        return "customer/customer_order";
    }

    @GetMapping("orderDetailPage")
    public String orderDetailPage(Integer orderId,Model model){
        model.addAttribute("order",customerOrderService.queryCustomerOrderByOrderId(orderId));
        return "customer/customer_order_detail";
    }

    @GetMapping("queryCustomerContributionByParams")
    @ResponseBody
    public Map<String,Object> queryCustomerContributionByParams(CustomerQuery customerQuery){
        return customerService.queryCustomerContributionByParams(customerQuery);
    }

    //客户构成曲线图
    @PostMapping("countCustomerMake")
    @ResponseBody
    public Map<String,Object> countCustomerMake(){
        return customerService.countCustomerMake();
    }


    //客户构成玫瑰图
    @PostMapping("countCustomerMake02")
    @ResponseBody
    public Map<String,Object> countCustomerMake02(){
        return customerService.countCustomerMake02();
    }

    //客户服务曲线图
    @PostMapping("countCustomerServe")
    @ResponseBody
    public Map<String,Object> countCustomerServe(){
        return customerService.countCustomerServe();
    }

    //客户服务玫瑰图
    @PostMapping("countCustomerServe02")
    @ResponseBody
    public Map<String,Object> countCustomerServe02(){
        return customerService.countCustomerServe02();
    }






}
